﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsWorm {
    internal class SearchManager {
        internal SearchManager() {
            m_Nodes = new List<TreeNode>();
            m_Results = new List<TreeNode>();
            m_OldReadNodes = new List<TreeNode>();
        }

        private List<TreeNode> m_Nodes;
        private List<TreeNode> m_Results;
        private List<TreeNode> m_OldReadNodes;

        internal List<TreeNode> OldNodes {
            get { return m_Nodes; }
        }

        internal List<TreeNode> OldReadNodes {
            get { return m_OldReadNodes; }
        }

        internal void SetBackupNodes( TreeNodeCollection Nodes ) {
            if ( m_Nodes.Count == 0 ) {
                foreach ( TreeNode node in Nodes )
                    m_Nodes.Add( node );
            }
        }

        internal void SetBackupReadNodes( List<TreeNode> Nodes ) {
            if ( m_OldReadNodes.Count == 0 ) {
                foreach ( TreeNode node in Nodes ) {
                    m_OldReadNodes.Add( node );
                }
            }
        }

        internal void ClearBackupNodes() {
            m_Nodes.Clear();
            m_OldReadNodes.Clear();
        }

        private bool SearchMatch( List<string> SearchTerms, string title ) {
            SearchTerms = SearchTerms.ConvertAll( term => term.ToLower() );
            title = title.ToLower();
            return SearchTerms.All( s => title.Contains( s ) );
        }

        internal List<TreeNode> PerformSearch( string SearchTerm, List<Feed> Feeds, Font FeedTreeFont,
            IFeedManager manager ) {
            m_Results.Clear();

            string[] SearchTermsArray = SearchTerm.Split( ' ' );
            List<string> SearchTerms = new List<string>();

            foreach ( string s in SearchTermsArray ) {
                if ( !s.Equals( "" ) )
                    SearchTerms.Add( s );
            }

            foreach ( Feed feed in Feeds ) {
                if ( !feed.IsCustomSearchFeed ) { //Do not search from custom feeds
                    TreeNode root = new TreeNode();
                    root.ToolTipText = feed.Description;
                    root.NodeFont = new Font( FeedTreeFont, FontStyle.Bold );
                    root.Name = feed.Title;
                    root.Text = feed.Title + "                    ";

                    foreach ( FeedItem item in feed.Items ) {
                        if ( SearchMatch( SearchTerms, item.Title ) ) {
                            TreeNode node = new TreeNode( item.Title );
                            node.Name = item.Url;
                            node.ToolTipText = item.Published;

                            if ( manager.VisitedItems.ContainsKey( item.Url ) )
                                node.ForeColor = Color.BlueViolet;
                            else
                                node.ForeColor = Color.Blue;

                            root.Nodes.Add( node );
                            m_Results.Add( root );
                        }
                    } //items

                } //iscustom

            } //feeds

            return m_Results;
        }

    }//class
}//namespace
